﻿using JSM;
using JSM.DataStore;
using JSM.MVC3.DataStore;

namespace System.Web.Mvc
{
	/// <summary>
	/// Extension methods for the use of a JavaScript Model.
	/// </summary>
	public static class WebViewPageExtensions
	{
		/// <summary>
		/// Renders the javascript for the Init-event.
		/// </summary>
		/// <typeparam name="TModel">
		/// The type of the view data model.
		/// </typeparam>
		/// <param name="webViewPage">
		/// The current <see cref="WebViewPage{TModel}"/>.
		/// </param>
		/// <param name="dataStoreProvider">
		/// An optional data store provider.
		/// </param>
		/// <returns>
		/// An <see cref="IHtmlString"/> containing javascript for the Init-event.
		/// </returns>
		public static IHtmlString RenderJavaScriptModelInit<TModel>(this WebViewPage<TModel> webViewPage, IDataStoreProvider dataStoreProvider = null)
		{
			return new MvcHtmlString(JSMHtmlHelper.GetInitJavaScript(null, new ViewDataDataStoreProvider(webViewPage.ViewData)));
		}

		/// <summary>
		/// Renders the javascript for the InitReady-event.
		/// </summary>
		/// <typeparam name="TModel">
		/// The type of the view data model.
		/// </typeparam>
		/// <param name="webViewPage">
		/// The current <see cref="WebViewPage{TModel}"/>.
		/// </param>
		/// <returns>
		/// An <see cref="IHtmlString"/> containing javascript for the Init-event.
		/// </returns>
		public static IHtmlString RenderJavaScriptModelInitReady<TModel>(this WebViewPage<TModel> webViewPage)
		{
			return new MvcHtmlString(JSMHtmlHelper.GetInitReadyJavaScript());
		}

		/// <summary>
		/// Renders a html container containing the javascript variables and javascript functions.
		/// </summary>
		/// <typeparam name="TModel">
		/// The type of the view data model.
		/// </typeparam>
		/// <param name="webViewPage">
		/// The current <see cref="WebViewPage{TModel}"/>.
		/// </param>
		/// <param name="dataStoreProvider">
		/// An optional data store provider.
		/// </param>
		/// <returns>
		/// An <see cref="IHtmlString"/> containing a html container containing the javascript variables and javascript functions.
		/// </returns>
		public static IHtmlString RenderJavaScriptModelContainer<TModel>(this WebViewPage<TModel> webViewPage, IDataStoreProvider dataStoreProvider = null)
		{
			return new MvcHtmlString(JSMHtmlHelper.GetJavaScriptModelContainer(null, new ViewDataDataStoreProvider(webViewPage.ViewData)));
		}
	}
}
